﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace ICommonLibrary.Security {

    public class Hasher {

        private static readonly SHA1 Sha = new SHA1CryptoServiceProvider();

        public static string SHA1Hex(string input) {
            byte[] hashedData = ComputeSHA1(Encoding.Unicode.GetBytes(input));
            return ToHex(hashedData);
        }

        public static string SHA1Base64(string input) {
            byte[] hashedData = ComputeSHA1(Encoding.Unicode.GetBytes(input));
            return Convert.ToBase64String(hashedData);
        }

        public static byte[] ComputeSHA1(byte[] input) {
            lock (Sha) {
                return Sha.ComputeHash(input);
            }
        }

        public static string ToHex(byte[] bytes) {
            var sum = new StringBuilder(bytes.Length * 2);
            for (int i = 0; i < bytes.Length; i++) {
                String hx = bytes[i].ToString("X2");
                sum.Append(hx);
            }
            return sum.ToString();
        }
    }
}